package com.logic.modular.gameCore.match.robot.service.impl;

import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logic.modular.gameCore.match.robot.mapper.CoreRobotTeamsMapper;
import com.comment.pojo.domain.logic.core.CoreRobotTeams;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * 人机编队(CoreRobotTeams)表服务实现类
 *
 * @author cxy
 * @since 2024-12-02 15:12:08
 */
@Slf4j
@Service
public class CoreRobotTeamsServiceImpl extends ServiceImpl<CoreRobotTeamsMapper, CoreRobotTeams>  {
    private static final  Cache<String, Map<Integer,CoreRobotTeams>> cache = Caffeine.newBuilder().build();
    
    @PostConstruct
    private void init() {
        CoreRobotTeams initSelect = CoreRobotTeams.builder().build();
        List<CoreRobotTeams> list = this.list(new LambdaQueryWrapper<>(initSelect));
        cache.put(
                "coreRobotTeams", 
                list.stream().collect(Collectors.toMap(CoreRobotTeams::getId, CoreRobotTeams::init))
        );
        log.info("人机编队数据缓存完成,数据量为:{}",list.size());
    }
    
    public static List<CoreRobotTeams> listCache() {
        return new ArrayList<>(mapCache().values());
    }     
    
    public static Map<Integer,CoreRobotTeams> mapCache() {
        Map<Integer,CoreRobotTeams> values = cache.getIfPresent("coreRobotTeams");
        if (Objects.isNull(values)) {
            throw new RuntimeException("人机编队数据为空");
        }
        return values;
    }   
    
    public static CoreRobotTeams getCache(Integer id) {
        return mapCache().get(id);
    }
}